package vistop.datatypes;

public class Edge extends GeometricalObject implements Comparable<Edge>{
	private int id;
	private Vertex start;
	private Vertex end;
	private boolean incomplete;
	
	public Edge(int id) {
		super(id);
		this.start = null;
		this.end = null;
		this.incomplete = true;
	}
	
	public Edge(int id, Vertex start, Vertex end) {
		super(id);
		this.start = start;
		this.end = end;
		this.incomplete = false;
	}
	
	public Edge(Vertex start, Vertex end) {
		super(start.getId() * 1000 + end.getId());
		this.start = start;
		this.end = end;
		this.incomplete = false;
	}

	public Vertex getStart() {
		return start;
	}
	
	public void setStart(Vertex start) {
		this.start = start;
		if (end != null) {
			this.incomplete = false;
		}
	}
	
	
	public Vertex getEnd() {
		return end;
	}
	
	public void setEnd(Vertex end) {
		this.end = end;
		if (start != null) {
			this.incomplete = false;
		}
	}
	
	public boolean isComplete() {
		return !incomplete;
	}
	
	public double getMinX() {
		return this.start.getX() < this.end.getX() ? this.start.getX() : this.end.getX();
	}
	
	public Vertex getMinXVertex() {
		return this.start.getX() < this.end.getX() ? this.start : this.end;
	}
	
	public Vertex getMaxXVertex() {
		return this.start.getX() > this.end.getX() ? this.start : this.end;
	}

	@Override
	public int compareTo(Edge arg0) {
		return Double.compare(this.getMinX(), arg0.getMinX());
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + id;
		result = prime * result + ((start == null) ? 0 : start.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Edge other = (Edge) obj;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (id != other.id)
			return false;
		if (start == null) {
			if (other.start != null)
				return false;
		} else if (!start.equals(other.start))
			return false;
		return true;
	}
	
	public Edge getEdge(int sign) {
		if (sign > 0) {
			return this;
		}
		else {
			return new Edge(id, end, start);
		}
	}
	
	public double getEdgeLen() {
		double x = start.getX() - end.getX();
		double y = start.getY() - end.getY();
		double z = start.getZ() - end.getZ();
		double dist = Math.sqrt(x*x + y*y + z*z);
		return dist;
	}
	
	@Override
	public String toString() {
		String result = "";
		if (incomplete)
			result = "incomplete";
		else
			result += "(" + this.start.toString() + "-> " + this.end.toString()+ ")";
		return result;
	}
}
